<template>
	<view class="content">
		<u-gap />
		<u-divider color="#2979ff" border-color="#2979ff">施工项目信息</u-divider>
		<u-form :label-width="180" label-align="right">
			<u-form-item label="施工单位">
				<u-input v-model="record.danwei" auto-height />
			</u-form-item>
			<u-form-item label="项目名称">
				<u-input v-model="record.name" type="textarea" />
			</u-form-item>
			<u-form-item label="项目周期">
				<u-input v-model="record.zhouqi" disabled @click="zhouqi_show=true" />
			</u-form-item>
			<u-form-item label="施工区域">
				<u-input v-model="record.quyu" />
			</u-form-item>
			<u-form-item label="经办人">
				<u-input v-model="record.jinbanren" />
			</u-form-item>
			<u-form-item label="经办人电话">
				<u-input v-model="record.jinbanren_phone" />
			</u-form-item>
            <u-form-item label="项目负责人">
                <u-input v-model="record.fuzeren" />
            </u-form-item>
            <u-form-item label="负责人电话">
                <u-input v-model="record.fuzeren_phone" />
            </u-form-item>
			<u-form-item label="安全员">
				<u-input v-model="record.safemen" />
			</u-form-item>
			<u-form-item label="安全员电话">
				<u-input v-model="record.safemen_phone" />
			</u-form-item>
			<u-form-item label="现场监理">
				<u-input v-model="record.jingli" />
			</u-form-item>
			<u-form-item label="现场监理电话">
				<u-input v-model="record.jingli_phone" />
			</u-form-item>
			<u-form-item label="车间监护">
				<u-input v-model="record.jianhu" />
			</u-form-item>
			<u-form-item label="车间监护电话">
				<u-input v-model="record.jianhu_phone" />
			</u-form-item>
			<u-form-item label="风险等级">
				<u-input v-model="record.fenxian_level" />
			</u-form-item>
			<u-form-item label="安全风险告知">
				<u-radio-group v-model="record.fenxian_gaozhi" @change="e=>record.fenxian_gaozhi=e" >
					<u-radio name="是">是</u-radio>
					<u-radio name="否">否</u-radio>
				</u-radio-group>
			</u-form-item>
			<u-form-item label="安全交底">
				<u-radio-group v-model="record.safe_jiaodi" @change="e=>record.safe_jiaodi=e" >
					<u-radio name="是">是</u-radio>
					<u-radio name="否">否</u-radio>
				</u-radio-group>
			</u-form-item>
		</u-form>

		<u-gap />
		<u-divider color="#2979ff" border-color="#2979ff">大型机械设备信息</u-divider>
		<u-gap />
		<u-card title="设备1" margin="0rpx">
			<template slot="body">
				<u-form :label-width="240" label-align="right" class="">
					<u-form-item label="设备名称">
						<u-input v-model="record.shebei1.name" />
					</u-form-item>
					<u-form-item label="型号、吨位">
						<u-input v-model="record.shebei1.category" />
					</u-form-item>
					<u-form-item label="检测合格证编号">
						<u-input v-model="record.shebei1.hege_no" />
					</u-form-item>
					<u-form-item label="检测报告有效期">
						<u-input v-model="record.shebei1.youxiaoqi" disabled @click="showCommonYouxiaoqi('shebei1')"/>
					</u-form-item>
					<u-form-item label="监理单位验收日期">
						<u-input v-model="record.shebei1.yanshou_date" disabled @click="shebei_yanshou1=true" />
					</u-form-item>
				</u-form>
			</template>
		</u-card>
		<u-card title="设备2" margin="0rpx">
			<template slot="body">
				<u-form :label-width="240" label-align="right" class="">
					<u-form-item label="设备名称">
						<u-input v-model="record.shebei2.name" />
					</u-form-item>
					<u-form-item label="型号、吨位">
						<u-input v-model="record.shebei2.category" />
					</u-form-item>
					<u-form-item label="检测合格证编号">
						<u-input v-model="record.shebei2.hege_no" />
					</u-form-item>
					<u-form-item label="检测报告有效期">
						<u-input v-model="record.shebei2.youxiaoqi" disabled @click="showCommonYouxiaoqi('shebei2')" />
					</u-form-item>
					<u-form-item label="监理单位验收日期">
						<u-input v-model="record.shebei2.yanshou_date" disabled @click="shebei_yanshou2=true" />
					</u-form-item>
				</u-form>
			</template>
		</u-card>
		<u-card title="设备3" margin="0rpx">
			<template slot="body">
				<u-form :label-width="240" label-align="right" class="">
					<u-form-item label="设备名称">
						<u-input v-model="record.shebei3.name" />
					</u-form-item>
					<u-form-item label="型号、吨位">
						<u-input v-model="record.shebei3.category" />
					</u-form-item>
					<u-form-item label="检测合格证编号">
						<u-input v-model="record.shebei3.hege_no" />
					</u-form-item>
					<u-form-item label="检测报告有效期">
						<u-input v-model="record.shebei3.youxiaoqi" disabled @click="showCommonYouxiaoqi('shebei3')" />
					</u-form-item>
					<u-form-item label="监理单位验收日期">
						<u-input v-model="record.shebei3.yanshou_date" disabled @click="shebei_yanshou3=true" />
					</u-form-item>
				</u-form>
			</template>
		</u-card>

		<u-gap />
		<u-divider color="#2979ff" border-color="#2979ff">重大节点备案情况</u-divider>
		<u-gap />
		<u-form :label-width="180" label-align="right">
			<u-form-item label="作业单位">
				<u-input v-model="record.zuoye_danwei" auto-height />
			</u-form-item>
			<u-form-item label="作业类型">
				<u-checkbox-group>
					<u-checkbox v-for="item in record.zuoye_category" v-model="item.checked" :name="item.name">{{item.name}}</u-checkbox>
				</u-checkbox-group>
			</u-form-item>
			<u-form-item label="作业时间">
				<u-input v-model="record.zuoye_date" disabled @click="zuoye_date_show=true" />
			</u-form-item>
			<u-form-item label="备案材料">
				<u-radio-group v-model="record.beian_cailiao" @change="e=>record.beian_cailiao=e" >
					<u-radio name="已提交">已提交</u-radio>
					<u-radio name="未提交">未提交</u-radio>
				</u-radio-group>
			</u-form-item>
			<u-form-item label="施工方案">
				<u-radio-group v-model="record.shigong_fanan" @change="e=>record.shigong_fanan=e" >
					<u-radio name="已提交">已提交</u-radio>
					<u-radio name="未提交">未提交</u-radio>
				</u-radio-group>
			</u-form-item>
			<u-form-item label="厂项目负责人">
				<u-input v-model="record.chang_fuze" auto-height />
			</u-form-item>
			<u-form-item label="联系电话">
				<u-input v-model="record.chang_fuze_phone" auto-height />
			</u-form-item>
			<u-form-item label="项目有无监理">
				<u-radio-group v-model="record.has_jianli" @change="e=>record.has_jianli=e" >
					<u-radio name="有">有</u-radio>
					<u-radio name="无">无</u-radio>
				</u-radio-group>
			</u-form-item>
		</u-form>

		<u-gap />
		<u-divider color="#2979ff" border-color="#2979ff">危险作业审批情况</u-divider>
		<u-gap />
		<u-form :label-width="180" label-align="right">
			<u-form-item label="作业类别">
				<u-checkbox-group>
					<u-checkbox v-for="item in record.danger.zuoye_category" v-model="item.checked" :name="item.name">{{item.name}}</u-checkbox>
				</u-checkbox-group>
			</u-form-item>
			<u-form-item label="作业时间">
				<u-input v-model="record.danger.zuoye_date" disabled @click="danger_zuoye_date_show=true" />
			</u-form-item>
			<u-form-item label="作业地点">
				<u-input v-model="record.danger.address" auto-height />
			</u-form-item>
			<u-form-item label="现场监护人员">
				<u-input v-model="record.danger.jianhu" auto-height />
			</u-form-item>
		</u-form>

		<u-card v-for="(item,index) in record.danger.worker" :key="index" :title="`作业人员${index+1}`" margin="0rpx">
			<template slot="body">
				<u-form :label-width="240" label-align="right" class="">
					<u-form-item label="姓名">
						<u-input v-model="item.name" />
					</u-form-item>
					<u-form-item label="特种作业名称">
						<u-input v-model="item.zuoye_name" />
					</u-form-item>
					<u-form-item label="特种作业证号">
						<u-input v-model="item.zuoye_no" />
					</u-form-item>
					<u-form-item label="有效期限">
						<u-input v-model="item.qixian" disabled @click="showZuoyeQixian(index)" />
					</u-form-item>
					<u-form-item class="sbbbb">
						<u-button @click="removeDangerWorder(index)" size="medium" type="error">删除该人员</u-button>
						<view style="display: inline-block;width: 10px;"></view>
						<u-button @click="addDangerWorder" size="medium" type="primary">添加人员</u-button>
					</u-form-item>
				</u-form>
			</template>
		</u-card>

		<u-calendar v-model="zhouqi_show" mode="range" max-date="2150-01-01" @change="zhouqiChange" />
		<u-calendar v-model="shebei_yanshou1" mode="date" max-date="2150-01-01" @change="e=>shebeiYanshouChange(e, 'shebei_yanshou1')" />
		<u-calendar v-model="shebei_yanshou2" mode="date" max-date="2150-01-01" @change="e=>shebeiYanshouChange(e, 'shebei_yanshou2')" />
		<u-calendar v-model="shebei_yanshou3" mode="date" max-date="2150-01-01" @change="e=>shebeiYanshouChange(e, 'shebei_yanshou3')" />
		<u-calendar v-model="zuoye_date_show" mode="range" max-date="2150-01-01" @change="zuoyeDateChange" />
		<u-calendar v-model="danger_zuoye_date_show" mode="range" max-date="2150-01-01" @change="dangerZuoyeDateChange" />
		<u-calendar v-model="shebei_youxiaoqi" mode="date" max-date="2150-01-01" @change="shebeiYouxiaoqiChange" />
		<u-calendar v-model="zuoye_qixian" mode="date" max-date="2150-01-01" @change="zuoyeQixianChange" />

        <view v-if="is_admin">
            <u-gap :height="90" />
            <view v-if="!id">
                <u-button type="success" :loading="submit_loading" @click="submit">提交</u-button>
            </view>
            <view style="text-align: center" v-else>
                <u-button size="medium" :loading="submit_loading" type="success" @click="submit">修改</u-button>
                <view style="width: 20px;display: inline-block"></view>
                <u-button size="medium" type="success" @click="jumpQrcode">查看二维码</u-button>
            </view>
        </view>

		<u-gap :height="45" />

	</view>
</template>

<script>
	export default {
		data() {
			return {
				is_admin:0,
				id:'',
				record:{
					danwei:'',
					name:'',
					zhouqi:'',
					quyu:'',
					jinbanren:'',
					jinbanren_phone:'',
					fuzeren:'',
					fuzeren_phone:'',
					safemen:'',
					safemen_phone:'',
					jingli:'',
					jingli_phone:'',
					jianhu:'',
					jianhu_phone:'',
					fenxian_level:'',
					fenxian_gaozhi:'否',
					safe_jiaodi:'否',
					shebei1:{
						name:'',
						category:'',
						hege_no:'',
						youxiaoqi:'',
						yanshou_date:''
					},
					shebei2:{
						name:'',
						category:'',
						hege_no:'',
						youxiaoqi:'',
						yanshou_date:''
					},
					shebei3:{
						name:'',
						category:'',
						hege_no:'',
						youxiaoqi:'',
						yanshou_date:''
					},
					zuoye_danwei:'',
					zuoye_category:[
						{name:'有限空间作业',checked:false},
						{name:'下井下池',checked:false},
						{name:'起重吊装',checked:false},
						{name:'脚手架搭拆',checked:false},
					],
					zuoye_date:'',
					beian_cailiao:'未提交',
					shigong_fanan:'未提交',
					chang_fuze:'',
					chang_fuze_phone:'',
					has_jianli:'无',
					danger:{
						zuoye_category: [
							{name:'动火一级',checked:false},
							{name:'动火二级',checked:false},
							{name:'动火三级',checked:false},
							{name:'登高一级',checked:false},
							{name:'登高二级',checked:false},
							{name:'登高三级',checked:false},
							{name:'登高特级',checked:false},
							{name:'起重',checked:false},
							{name:'倒闸操作',checked:false},
							{name:'带电作业',checked:false},
							{name:'应急抢修',checked:false},
							{name:'抽堵盲板',checked:false},
							{name:'电动葫芦',checked:false},
							{name:'有限空间',checked:false},
							{name:'动土作业',checked:false},
							{name:'其他',checked:false},
						],
						zuoye_date:'',
						address:'',
						jianhu:'',
						worker:[
							{name:'',zuoye_name:'',zuoye_no:'',qixian:''}
						],
					},
				},
				zhouqi_show:false,
				shebei_yanshou1:false,
				shebei_yanshou2:false,
				shebei_yanshou3:false,
				zuoye_date_show:false,
				danger_zuoye_date_show:false,
				submit_loading:false,
				shebei_youxiaoqi:false,
				shebei_youxiaoqi_index:'',
				zuoye_qixian:false,
				zuoye_qixian_index:'',
			}
		},
		onLoad(e) {
			if (e.id) {
				console.log(e.id)
				this.id = e.id
				this.fetchData(e.id)
			}
			if (e.scene) {
				console.log(e.scene)
				this.id = e.scene
				this.fetchData(e.scene)
			}
		},
		onShow(){
			//判断是否是管理员
			let is_admin = uni.getStorageSync("is_admin")
			if (is_admin) {
				this.is_admin = 1
			}
		},
		methods: {
			fetchData(id) {
				if (!id) {
					return
				}
				uni.showLoading({title:'正在获取数据...'})
				uniCloud.callFunction({
					name:'record_get',
					data:{
						id
					}
				}).then(res=>{
					console.log(res)
					this.record = res.result
					uni.hideLoading()
				}).catch(e=>{
					uni.showModal({title:'错误提示', content:e})
					uni.hideLoading()
				})
			},
			zhouqiChange(e) {
				this.record.zhouqi = e.startDate+" ~ "+e.endDate
			},
			zuoyeDateChange(e) {
				this.record.zuoye_date = e.startDate+" ~ "+e.endDate
			},
			dangerZuoyeDateChange(e) {
				this.record.danger.zuoye_date = e.startDate+" ~ "+e.endDate
			},
			shebeiYanshouChange(e, name) {
				switch (name) {
					case 'shebei_yanshou1':
						this.record.shebei1.yanshou_date = e.result
                        break;
					case 'shebei_yanshou2':
						this.record.shebei2.yanshou_date = e.result
						break;
					case 'shebei_yanshou3':
						this.record.shebei3.yanshou_date = e.result
						break;
				}
			},
			addDangerWorder() {
				this.record.danger.worker.push({name:'',zuoye_name:'',zuoye_no:'',qixian:''})
			},
			removeDangerWorder(idx) {
				if (this.record.danger.worker.length == 1) {
					uni.showModal({title:'提示', content:'至少保留一项工作人员'})
					return
				}
				this.record.danger.worker = this.record.danger.worker.filter((v,k)=>k!=idx)
			},
			submit() {
				this.submit_loading = true
				uniCloud.callFunction({
					name:'record_save',
					data:{
						id:this.id,
                        data:this.record
                    }
				}).then(res=>{
					this.id = res.result
					this.submit_loading = false
					uni.showModal({title:'提示', content:'操作成功'})
				}).catch(e=>{
					uni.showModal({title:'错误提示', content:e})
					this.submit_loading = false
				})
			},
			jumpQrcode() {
				uni.navigateTo({url:'/pages/qrcode/qrcode?id='+this.id})
			},
			showCommonYouxiaoqi(index){
				this.shebei_youxiaoqi = true
				this.shebei_youxiaoqi_index = index
			},
			shebeiYouxiaoqiChange(e){
				this.record[this.shebei_youxiaoqi_index].youxiaoqi = e.result
			},
			showZuoyeQixian(index) {
				this.zuoye_qixian = true
				this.zuoye_qixian_index = index
			},
			zuoyeQixianChange(e){
				this.record.danger.worker[this.zuoye_qixian_index].qixian = e.result
			}
		}
	}
</script>

<style>
	.content {
		padding: 0 10px;
	}
	.shebei-box {
		padding: 10px;
	}
	.sbbbb {
		text-align: center;
	}
</style>
